package com.dtw.ditouwang.ui.movement;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.weather.LocalWeatherForecastResult;
import com.amap.api.services.weather.LocalWeatherLiveResult;
import com.amap.api.services.weather.WeatherSearch;
import com.amap.api.services.weather.WeatherSearchQuery;
import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.ChallengeListBean;
import com.dtw.ditouwang.beans.MovementBean;
import com.dtw.ditouwang.beans.MovementFragmentBean;
import com.dtw.ditouwang.databinding.FragmentMovementBinding;
import com.dtw.ditouwang.repositories.UserRepository;
import com.dtw.ditouwang.ui.BaseFragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.dtw.ditouwang.ui.movement.MovementChallengeActivity.KeyChallengeBean;

public class MovementFragment extends BaseFragment implements View.OnClickListener {
    FragmentMovementBinding fragmentMovementBinding;
    /**
     * 健走
     */
    private RadioButton radioWalk;
    /**
     * 跑步
     */
    private RadioButton radioRun;
    /**
     * 室内
     */
    private RadioButton radioRoom;
    /**
     * 室外
     */
    private RadioButton radioOut;
    private TextView weather;
    AMapLocationClient aMapLocationClient;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentMovementBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_movement, container, false);
        fragmentMovementBinding.setView(this);
        return fragmentMovementBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        radioWalk = view.findViewById(R.id.radio_walk);
        radioRun = view.findViewById(R.id.radio_run);
        radioRoom = view.findViewById(R.id.radio_room);
        radioOut = view.findViewById(R.id.radio_out);
        weather = view.findViewById(R.id.text_weather);
        radioWalk.setOnClickListener(this);
        radioRun.setOnClickListener(this);
        radioRoom.setOnClickListener(this);
        radioOut.setOnClickListener(this);
        initView();
    }

    @Override
    public void onStart() {
        super.onStart();
        getWeather();
    }

    public void click(View view) {
        switch (view.getId()) {
            case R.id.text_start:
                if (radioOut.isChecked()) {
                    ChallengeListBean.DataBean challengeListBean = new ChallengeListBean.DataBean();
                    challengeListBean.setChallenge(false);
                    challengeListBean.setWalk(radioWalk.isChecked());
                    Intent out = new Intent(getContext(), MovementOutSideActivity.class);
                    out.putExtra(MovementChallengeActivity.KeyChallengeBean, challengeListBean);
                    startActivity(out);
                } else {
                    ChallengeListBean.DataBean challengeListBean = new ChallengeListBean.DataBean();
                    challengeListBean.setChallenge(false);
                    challengeListBean.setWalk(radioWalk.isChecked());
                    Intent out = new Intent(getContext(), MovementInsideActivity.class);
                    out.putExtra(MovementChallengeActivity.KeyChallengeBean, challengeListBean);
                    startActivity(out);
                }
                break;
            case R.id.text_challenge:
                int roomType = 1, walkType = 1;
                if (radioRoom.isChecked()) {
                    roomType = 0;
                }
                if (radioWalk.isChecked()) {
                    walkType = 0;
                }
                ChallengeListBean.DataBean challengeListBean = new ChallengeListBean.DataBean();
                challengeListBean.setChallenge(false);
                challengeListBean.setWalk(radioWalk.isChecked());
                Intent intent = new Intent(getContext(), MovementChallengeActivity.class);
                intent.putExtra(MovementChallengeActivity.KeyTypeRoom, roomType);
                intent.putExtra(MovementChallengeActivity.KeyTypeWalk, walkType);
                intent.putExtra(MovementChallengeActivity.KeyChallengeBean, challengeListBean);
                startActivity(intent);
                break;
        }
    }

    public void initView() {
        int walk = 1;
        if (!radioWalk.isChecked()) {
            walk = 2;
        }
        webApi.getMovemnt(UserRepository.getInstance().getUserID(), walk).enqueue(new Callback<MovementFragmentBean>() {
            @Override
            public void onResponse(Call<MovementFragmentBean> call, Response<MovementFragmentBean> response) {
                if (response.body()!=null&&response.body().isResult()) {
                    if (radioWalk.isChecked()) {
                        response.body().getData().setType("健走");
                        response.body().getData().setButtonText("开始健走");
                    } else {
                        response.body().getData().setType("跑步");
                        response.body().getData().setButtonText("开始跑步");
                    }
                    fragmentMovementBinding.setMovement(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<MovementFragmentBean> call, Throwable t) {

            }
        });
    }

    public void getWeather() {
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        mLocationOption.setOnceLocation(true);
        aMapLocationClient = new AMapLocationClient(getContext());
        aMapLocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                WeatherSearchQuery mquery = new WeatherSearchQuery(aMapLocation.getCity(), WeatherSearchQuery.WEATHER_TYPE_LIVE);
                WeatherSearch mweathersearch = new WeatherSearch(getContext());
                mweathersearch.setOnWeatherSearchListener(new WeatherSearch.OnWeatherSearchListener() {
                    @Override
                    public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int i) {
                        weather.setText(localWeatherLiveResult.getLiveResult().getWeather() + "  " + localWeatherLiveResult.getLiveResult().getTemperature() + "℃");
                        Log.i("dtw", "weather：" + localWeatherLiveResult.getLiveResult().getCity() + localWeatherLiveResult.getLiveResult().getWeather());
                    }

                    @Override
                    public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) {

                    }
                });
                mweathersearch.setQuery(mquery);
                mweathersearch.searchWeatherAsyn(); //异步搜索
            }
        });
        aMapLocationClient.startLocation();
    }

    @Override
    public void onStop() {
        super.onStop();
        aMapLocationClient.onDestroy();
    }

    public static void main(String[] arg){
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(dateFormat.format(System.currentTimeMillis()));

    }

    @Override
    public void onClick(View view) {
        if (view instanceof CompoundButton) {
            if (((CompoundButton) view).isChecked())
                initView();
        }

    }
}
